﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MIDIDrum
{
    public partial class SettingsForm : Form
    {
        Nini.Ini.IniDocument doc = new Nini.Ini.IniDocument("settings.ini");
        public SettingsForm()
        {
            InitializeComponent();
            RefreshListBox();
        }

        void RefreshListBox()
        {
            listBox.Items.Clear();
            Nini.Ini.IniSection sect = doc.Sections["SOUNDS"];
            for (int i = 0; i < sect.ItemCount; i++)
            {
                Nini.Ini.IniItem item = sect.GetItem(i);
                if (!item.Name.StartsWith("#comment"))
                    listBox.Items.Add(string.Format("{0} = {1}", item.Name, item.Value));
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            pnlValues.Visible = true;
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            int i = 0;
            if (int.TryParse(txtKey.Text, out i))
            {
                doc.Sections["SOUNDS"].Set(txtKey.Text, txtValue.Text);
            }
            pnlValues.Visible = false;
            txtValue.Text = "";
            txtKey.Text = "";
            RefreshListBox();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            int index = listBox.SelectedIndex;
            Nini.Ini.IniItem item = doc.Sections["SOUNDS"].GetItem(index);
            doc.Sections["SOUNDS"].Remove(item.Name);
            RefreshListBox();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
            doc.Save("settings.ini");
        }
    }
}
